package cn.ibizlab.odoo.core.odoo_stock.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Istock_inventory_line;
import cn.ibizlab.odoo.core.odoo_stock.domain.Stock_inventory_line;
import cn.ibizlab.odoo.core.odoo_stock.filter.Stock_inventory_lineSearchContext;
import cn.ibizlab.odoo.core.odoo_stock.service.IStock_inventory_lineService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[库存明细] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Stock_inventory_lineServiceImpl implements IStock_inventory_lineService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Istock_inventory_lineClientService stock_inventory_lineClientService;

    @Override
    public boolean create(Stock_inventory_line stock_inventory_line) {
        boolean bOk = false ;
        Istock_inventory_line clientModel = this.convert2Model(stock_inventory_line,null);
        stock_inventory_lineClientService.create(clientModel) ;
        this.convert2Domain(clientModel,stock_inventory_line);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Stock_inventory_line get(Integer id) {
        Istock_inventory_line clientModel = stock_inventory_lineClientService.createModel();
        clientModel.setId(id);
        stock_inventory_lineClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Stock_inventory_line get(Stock_inventory_line stock_inventory_line) {
        Stock_inventory_line retstock_inventory_line = this.get(stock_inventory_line.getId()) ;
    	BeanCopier copier=BeanCopier.create(retstock_inventory_line.getClass(), stock_inventory_line.getClass(), false);
        copier.copy(retstock_inventory_line, stock_inventory_line, null);
        return stock_inventory_line ;
    }

    @Override
    public boolean update(Stock_inventory_line stock_inventory_line) {
        boolean bOk = false ;
        Istock_inventory_line clientModel = this.convert2Model(stock_inventory_line,null);
        stock_inventory_lineClientService.update(clientModel) ;
        this.convert2Domain(clientModel,stock_inventory_line) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //stock_inventory_lineClientService.removeBatch(stock_inventory_lines) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Istock_inventory_line clientModel = stock_inventory_lineClientService.createModel();
        clientModel.setId(id);
        stock_inventory_lineClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Stock_inventory_line> stock_inventory_lines, int batchSize) {

                        //stock_inventory_lineClientService.createBatch(stock_inventory_lines,batchSize) ;

    }

    @Override
    public void updateBatch(List<Stock_inventory_line> stock_inventory_lines, int batchSize) {

        //stock_inventory_lineClientService.updateBatch(stock_inventory_lines,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Stock_inventory_line> searchDefault(Stock_inventory_lineSearchContext context) {
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据产品查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_product(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_company(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_partner(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_create(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_write(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_inventory(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_location(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据批次/序列号查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_prod_lot(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据包裹查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_package(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品计量单位查询
     */
    @Override
    public Page<Stock_inventory_line> selectByOdoo_product_uom(Integer id){
        Stock_inventory_lineSearchContext context = new Stock_inventory_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_inventory_line> list = new ArrayList<Stock_inventory_line>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_inventory_line> clientModelList = stock_inventory_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_inventory_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_inventory_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Istock_inventory_line convert2Model(Stock_inventory_line domain , Istock_inventory_line model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = stock_inventory_lineClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getTheoretical_qtyDirtyFlag())
            model.setTheoretical_qty(domain.getTheoretical_qty());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getProduct_qtyDirtyFlag())
            model.setProduct_qty(domain.getProduct_qty());
         if(domain.getPackage_id_textDirtyFlag())
            model.setPackage_id_text(domain.getPackage_id_text());
         if(domain.getProduct_uom_category_idDirtyFlag())
            model.setProduct_uom_category_id(domain.getProduct_uom_category_id());
         if(domain.getInventory_id_textDirtyFlag())
            model.setInventory_id_text(domain.getInventory_id_text());
         if(domain.getInventory_location_idDirtyFlag())
            model.setInventory_location_id(domain.getInventory_location_id());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getLocation_id_textDirtyFlag())
            model.setLocation_id_text(domain.getLocation_id_text());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getProduct_uom_id_textDirtyFlag())
            model.setProduct_uom_id_text(domain.getProduct_uom_id_text());
         if(domain.getProd_lot_id_textDirtyFlag())
            model.setProd_lot_id_text(domain.getProd_lot_id_text());
         if(domain.getProduct_trackingDirtyFlag())
            model.setProduct_tracking(domain.getProduct_tracking());
         if(domain.getLocation_idDirtyFlag())
            model.setLocation_id(domain.getLocation_id());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
         if(domain.getProd_lot_idDirtyFlag())
            model.setProd_lot_id(domain.getProd_lot_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getPackage_idDirtyFlag())
            model.setPackage_id(domain.getPackage_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getInventory_idDirtyFlag())
            model.setInventory_id(domain.getInventory_id());
         if(domain.getProduct_uom_idDirtyFlag())
            model.setProduct_uom_id(domain.getProduct_uom_id());
        return model ;
    }

    public Stock_inventory_line convert2Domain( Istock_inventory_line model ,Stock_inventory_line domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Stock_inventory_line();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getTheoretical_qtyDirtyFlag())
            domain.setTheoretical_qty(model.getTheoretical_qty());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getProduct_qtyDirtyFlag())
            domain.setProduct_qty(model.getProduct_qty());
        if(model.getPackage_id_textDirtyFlag())
            domain.setPackage_id_text(model.getPackage_id_text());
        if(model.getProduct_uom_category_idDirtyFlag())
            domain.setProduct_uom_category_id(model.getProduct_uom_category_id());
        if(model.getInventory_id_textDirtyFlag())
            domain.setInventory_id_text(model.getInventory_id_text());
        if(model.getInventory_location_idDirtyFlag())
            domain.setInventory_location_id(model.getInventory_location_id());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getLocation_id_textDirtyFlag())
            domain.setLocation_id_text(model.getLocation_id_text());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getProduct_uom_id_textDirtyFlag())
            domain.setProduct_uom_id_text(model.getProduct_uom_id_text());
        if(model.getProd_lot_id_textDirtyFlag())
            domain.setProd_lot_id_text(model.getProd_lot_id_text());
        if(model.getProduct_trackingDirtyFlag())
            domain.setProduct_tracking(model.getProduct_tracking());
        if(model.getLocation_idDirtyFlag())
            domain.setLocation_id(model.getLocation_id());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        if(model.getProd_lot_idDirtyFlag())
            domain.setProd_lot_id(model.getProd_lot_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getPackage_idDirtyFlag())
            domain.setPackage_id(model.getPackage_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getInventory_idDirtyFlag())
            domain.setInventory_id(model.getInventory_id());
        if(model.getProduct_uom_idDirtyFlag())
            domain.setProduct_uom_id(model.getProduct_uom_id());
        return domain ;
    }


}
